package com.leetcode.partition12;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2022/1/29 13:36
 */
public class LC1192查找集群内的关键连接 {

    private static final int N = (int) (1e5 + 10), M = (int) (3e5 + 10);
    private static int[] h = new int[N], e = new int[M], ne = new int[M];

    private static int n = 0, m = 0, idx = 0, timestamp = 0, eDccCnt = 0;
    private static int[] eDcc = new int[N], dfn = new int[N], low = new int[N];
    private static boolean[] isBridge = new boolean[M];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(reader.readLine());
        List<List<Integer>> connections = new ArrayList<>();
        for (;;) {
            String[] s = reader.readLine().split(" ");
            int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]);
            if (a == -1 && b == -1) break;
            List<Integer> edge = new ArrayList<>();
            edge.add(a);
            edge.add(b);
            connections.add(edge);
        }
        List<List<Integer>> answer = criticalConnections(n, connections);
        for (List<Integer> ans : answer) writer.write(ans.get(0) + " " + ans.get(1) + "\n");
        writer.flush();
    }

    public static List<List<Integer>> criticalConnections(int _n, List<List<Integer>> connections) {
        n = _n;
        m = connections.size() * 2;
        List<List<Integer>> answer = new ArrayList<>();
        for (int i = 0; i <= n; i++) h[i] = -1;
        for (List<Integer> connection : connections) {
            int a = connection.get(0), b = connection.get(1);
            add(a, b);
            add(b, a);
        }
        tarjan(1, -1);
        for (int i = 0; i < m; i += 2) {
            if (isBridge[i]) {
                List<Integer> ans = new ArrayList<>(2);
                ans.add(e[i ^ 1]);
                ans.add(e[i]);
                answer.add(ans);
            }
        }
        return answer;
    }

    private static void tarjan(int u, int inBridge) {
        dfn[u] = low[u] = ++timestamp;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v, i);
                low[u] = Math.min(low[u], low[v]);
                if (dfn[u] < low[v]) isBridge[i] = isBridge[i ^ 1] = true;
            } else if (i != (inBridge ^ 1)) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
